package com.huawei.petSystemdemo.data;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huawei.petSystemdemo.bean.Order;
import com.huawei.petSystemdemo.bean.Pets;
import com.huawei.petSystemdemo.bean.Shop;
import com.huawei.petSystemdemo.utils.MyRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

public class OrderRepository {
    public static void getAll(int current, int size, Consumer<List<Order>> callback) {
        Map<String, Object> body = new HashMap<>();
        body.put("size", size);
        body.put("current", current);
        MyRequest.get("/order/getAll", body, json -> {
            JSONObject obj = JSON.parseObject(json);
            boolean success = obj.getBoolean("success");
            if (success)
            {
                List<Order> shopList = obj.getJSONArray("data").toJavaList(Order.class);
                if (callback != null)
                    callback.accept(shopList);
            }
            else
            {

            }
        });
    }

    public static void getOrder(int user_id, Consumer<List<Order>> callback) {
        Map<String, Object> res = new HashMap<>();
        res.put("user_id", user_id);
        MyRequest.get("/order/getOrder", res, json -> {
            JSONObject obj = JSON.parseObject(json);
            boolean success = obj.getBoolean("success");
            if (success)
            {
                List<Order> orderList = obj.getJSONArray("data").toJavaList(Order.class);
                if (callback != null)
                    callback.accept(orderList);
            }
            else
            {

            }
        });
    }

    public static void getPetByOrder(int orderId, Consumer<List<Pets>> callback) {
        Map<String, Object> res = new HashMap<>();
        res.put("orderId", orderId);
        MyRequest.get("/orderCommodity/getAll", res, json -> {
            JSONObject obj = JSON.parseObject(json);
            boolean success = obj.getBoolean("success");
            if (success)
            {
                List<Pets> petsList = obj.getJSONArray("data").toJavaList(Pets.class);
                if (callback != null)
                    callback.accept(petsList);
            }
            else
            {

            }
        });
    }

    public static void getOrderByState(int state, int user_id, Consumer<List<Order>> callback) {
        Map<String, Object> res = new HashMap<>();
        res.put("user_id", user_id);
        res.put("state", state);
        MyRequest.get("/order/getByState", res, json -> {
            JSONObject obj = JSON.parseObject(json);
            boolean success = obj.getBoolean("success");
            if (success)
            {
                List<Order> orderList = obj.getJSONArray("data").toJavaList(Order.class);
                if (callback != null)
                    callback.accept(orderList);
            }
            else
            {

            }
        });
    }


}
